﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Text;
using System.Data;

public partial class Evaluate_AllDetailD : System.Web.UI.Page
{
    private int dcount;
    private int tlength = 16;
    private int row = 1;
    private decimal temp = 0;
    private decimal[] total = new decimal[100];
    protected void Page_Load(object sender, EventArgs e)
    {

        CreateTable();
    }


    private void CreateTable()
    {
        for (int i = 0; i < dcount; i++)
        {
            total[i] = 0;
        }
        string Type = Request["T"];
        string departmentId = Request["DepartmentId"];
        string sql = "SELECT ShortName,DepartmentID FROM Account_Departments WHERE DepartmentId like '" +
                   departmentId.Substring(0, 4) + Type + "%' order By DepartmentID";
        DataView dv = SplitPage.ExecuteSQL(sql).Tables[0].DefaultView;
        dcount = dv.Count;
        TableRow tr = new TableRow();
        tr.CssClass = "grid_header";
        tr.HorizontalAlign = HorizontalAlign.Right;
        //月份
        TableCell tc = new TableCell();
        tc.CssClass = "Td";
        tc.Text = "月份";

        tr.Cells.Add(tc);


        tc = new TableCell();
        tc.CssClass = "Td";
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.Text = "项目";
        tr.Cells.Add(tc);
        foreach (DataRowView drv in dv)
        {
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Right;
  
            tc.Text = drv["ShortName"].ToString();
            tr.Cells.Add(tc);
        }
        tb.Rows.Add(tr);
        int startMonth = Convert.ToInt32(Request.QueryString["Sm"]);
        int endMonth = Convert.ToInt32(Request.QueryString["Em"]);
        for (int i = startMonth; i <= endMonth; i++)
        {
            TableCell mTc = new TableCell();
            mTc.CssClass = "Td";
            int col = 0;
            mTc.Text = i.ToString();

            col = BindTaskTable(i, col, mTc);
            col = BindBaseTable(dv, i, col, mTc);
            col = BindInspectTable(i, col, mTc);
            col = BindCheckTable(i, col, mTc);
            col = BindAwardTable(i, col, mTc);
            mTc.RowSpan = col;
        }
        BindChart();
        BindTotalTable();
    }
    private int BindTaskTable(int mon, int ros, TableCell mTc)
    {
        if (Request["Task"] != null)
        {
            string Type = Request["T"];
            string departmentid = Request["DepartmentId"];
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("DECLARE @SQL nvarchar(max)");
            sql.AppendLine("SET @SQL = ''");
            sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN UserId = ''' + DepartmentId + ''' THEN Score END) AS D' + DepartmentId FROM Account_Departments WHERE Departmentid like '" + departmentid.Substring(0, 4) + Type + "%' order by departmentid ");
            sql.AppendLine("SET @SQL = 'SELECT TaskId ' +@SQL + ' FROM TaskUsers WHERE taskid in(select taskid from tasks where YEAR(EndDate) =" + Request["year"] + " AND MONTH(EndDate)=" + mon + " and TaskId IN (" + Request["Task"].Substring(0, Request.QueryString["Task"].Length - 1) + ")) GROUP BY TaskId' ");
            sql.AppendLine("EXEC(@SQL)");
            DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
            TableCell tc;
            string title = "";
            foreach (DataRowView drv in dv)
            {
                TableRow tr = new TableRow();
                tr.CssClass = "grid_item";
                tr.HorizontalAlign = HorizontalAlign.Right;

                if (ros == 0)
                {
                    tr.Cells.Add(mTc);
                }
                ros++;
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.HorizontalAlign = HorizontalAlign.Left;
                title = row.ToString() + "、" + Task.GetTask((int)drv[0]).Title;
                if (title.Length > tlength)
                    tc.Text = title.Substring(0, tlength) + "...";
                else
                    tc.Text = title;
                tr.Cells.Add(tc);


                for (int i = 1; i <= dcount; i++)
                {
                    tc = new TableCell();
                    tc.CssClass = "Td";
                    tc.HorizontalAlign = HorizontalAlign.Right;
                    tc.Width = Unit.Pixel(40);
                    string RootId = drv[0].ToString();
                    string UserId = dv.Table.Columns[i].ColumnName.Replace("D", "");
                    if (drv[i] != DBNull.Value)
                    {
                        string Text = decimal.Parse(drv[i].ToString()).ToString("0");
                        tc.Text = "<a href=TaskView.aspx?RootId=" + RootId + "&UserId=" + UserId + ">" + Text + "</a>";
                    }
                    else
                    {

                        tc.Text = "<a href=TaskView.aspx?RootId=" + RootId + "&UserId=" + UserId + ">未审核</a>";
                    }
                    tr.Cells.Add(tc);
                    if (drv[i] != DBNull.Value)
                    {
                        total[i - 1] += (decimal)drv[i];
                    }
                }
                tb.Rows.Add(tr);
                row++;
            }


        }
        return ros;
    }
    private int BindBaseTable(DataView dvv, int mon, int ros, TableCell mTc)
    {
        if (Request["Base"] != null)
        {
            string Type = Request["T"];
            string departmentid = Request["DepartmentId"];
            string[] strBases = Request["Base"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string strBase in strBases)
            {
                string[] bases = strBase.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                if (bases[0] == mon.ToString())
                {
                    StringBuilder sql = new StringBuilder();
                    sql.AppendLine("DECLARE @SQL nvarchar(max)");
                    sql.AppendLine("SET @SQL = ''");
                    sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '000' FROM Account_Departments WHERE Departmentid like '" + departmentid.Substring(0, 4) + Type + "%' order by departmentid ");
                    sql.AppendLine("SET @SQL = 'SELECT a.BaseID, a.BaseName' + @SQL + ' FROM Base a LEFT JOIN BaseItems b ON a.BaseID = b.BaseID WHERE (b.YearValue=" + Request["Year"] + " and b.MonthValue=" + mon + ") and a.BaseID in (" + bases[1] + ")  GROUP BY a.BaseID, a.BaseName'");
                    sql.AppendLine("EXEC(@SQL)");

                    DataTable dt = SplitPage.ExecuteSQL(sql.ToString()).Tables[0];
                    DataRow drv = null;
                    if (dt.Rows.Count > 0)
                    {
                        drv = dt.Rows[0];
                    }
                    else
                    {
                        string baseSql = "select * from Base where BaseID=" + bases[1];
                        DataRow baseInfo = SplitPage.ExecuteSQL(baseSql).Tables[0].Rows[0];
                        drv = dt.NewRow();
                        drv[0] = baseInfo[0];
                        drv[1] = baseInfo[1];
                        for (int i = 2; i < dt.Columns.Count; i++)
                        {
                            drv[i] = 0;
                        }
                    }

                    TableRow tr = new TableRow();
                    tr.CssClass = "grid_item";
                    tr.HorizontalAlign = HorizontalAlign.Right;
                    TableCell tc = new TableCell();


                    tr = new TableRow();
                    tr.CssClass = "grid_item";

                    if (ros == 0)
                    {
                        tr.Cells.Add(mTc);
                    }
                    ros++;




                    tc = new TableCell();
                    tc.CssClass = "Td";
                    tc.HorizontalAlign = HorizontalAlign.Left;
                    tc.Text = row.ToString() + "、" + Base.GetBase((int)drv[0]).BaseName;
                    tr.Cells.Add(tc);

                    for (int i = 2; i <= dcount + 1; i++)
                    {
                        tc = new TableCell();
                        tc.CssClass = "Td";
                        tc.BackColor = System.Drawing.Color.White;
                        tc.HorizontalAlign = HorizontalAlign.Right;
                        if (drv[i] != DBNull.Value)
                            temp = Base.GetBase((int)drv[0]).TotalScore - (decimal)drv[i];
                        else
                            temp = Base.GetBase((int)drv[0]).TotalScore;
                        total[i - 2] += temp;
                        tc.Text = "<a href=BaseView.aspx?BaseId=" + drv[0].ToString() + "&DepartmentId=" + dt.Columns[i].ColumnName.Replace("D", "") + "&Year=2008&month=" + mon + "&EndMonth=" + mon + ">" + temp.ToString("0") + "</a>";
                        tr.Cells.Add(tc);
                    }
                    tb.Rows.Add(tr);
                    row++;
                }
            }
        }
        return ros;
    }


    private int BindInspectTable(int mon, int ros, TableCell mTc)
    {

        if (Request["Inspect"] != null)
        {
            string Type = Request["T"];
            string departmentid = Request["DepartmentId"];
            string Data = Request["Inspect"];
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("DECLARE @SQL nvarchar(max)");
            sql.AppendLine("SET @SQL = ''");
            sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '000' FROM Account_Departments WHERE Departmentid like '" + departmentid.Substring(0, 4) + Type + "%' order by departmentid ");
            sql.AppendLine("SET @SQL = 'SELECT a.InspectID, a.InspectName' + @SQL + ' FROM Inspects a LEFT JOIN InspectItems b ON a.InspectID = b.InspectID WHERE YEAR(a.InspectDate)=" + Request["Year"] + " AND MONTH(a.InspectDate)=" + mon
     + " and a.InspectID in (" + Data.Substring(0, Data.Length - 1) + ") GROUP BY a.InspectID, a.InspectName, a.InspectDate ORDER BY a.InspectDate'");
            sql.AppendLine("EXEC(@SQL)");
            DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
            TableRow tr;
            TableCell tc;
            string title = "";

            foreach (DataRowView drv in dv)
            {
                tr = new TableRow();
                tr.CssClass = "grid_item";

                if (ros == 0)
                {
                    tr.Cells.Add(mTc);
                }
                ros++;




                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                title = row.ToString() + "、" + Inspect.GetInspect((int)drv[0]).InspectName;
                if (title.Length > tlength)
                    tc.Text = title.Substring(0, tlength) + "...";
                else
                    tc.Text = title;
                tr.Cells.Add(tc);
                for (int i = 2; i <= dcount + 1; i++)
                {
                    tc = new TableCell();
                    tc.CssClass = "Td";
                    tc.BackColor = System.Drawing.Color.White;
                    tc.HorizontalAlign = HorizontalAlign.Right;
                    if (drv[i] != DBNull.Value)
                        temp = Inspect.GetInspect((int)drv[0]).TotalScore - (decimal)drv[i];
                    else
                        temp = Inspect.GetInspect((int)drv[0]).TotalScore;
                    total[i - 2] += temp;
                    tc.Text = "<a href=InspectView.aspx?InspectId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + temp.ToString("0") + "</a>";
                    tr.Cells.Add(tc);
                }
                tb.Rows.Add(tr);
                row++;
            }
        }
        return ros;
    }
    private int BindCheckTable(int mon, int ros, TableCell mTc)
    {
        if (Request["Check"] != null)
        {
            string Type = Request["T"];
            string departmentid = Request["DepartmentId"];
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("DECLARE @SQL nvarchar(max)");
            sql.AppendLine("SET @SQL = ''");
            sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '000' FROM Account_Departments WHERE Departmentid like '" + departmentid.Substring(0, 4) + Type + "%' order by departmentid ");
            sql.AppendLine("SET @SQL = 'SELECT a.CheckID, a.CheckName' + @SQL + ' FROM Checks a LEFT JOIN CheckItems b ON a.CheckID = b.CheckID WHERE YEAR(a.CheckDate)=" + Request["Year"] + " AND MONTH(a.CheckDate)=" + mon + " and a.CheckID in (" + Request["Check"].Substring(0, Request["Check"].Length - 1) + ") GROUP BY a.CheckID, a.CheckName, a.CheckDate ORDER BY a.CheckDate'");
            sql.AppendLine("EXEC(@SQL)");
            DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
            TableRow tr;
            TableCell tc;
            string title = "";
            foreach (DataRowView drv in dv)
            {
                tr = new TableRow();
                tr.CssClass = "grid_item";

                if (ros == 0)
                {
                    tr.Cells.Add(mTc);
                }
                ros++;



                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                title = row.ToString() + "、" + Check.GetCheck((int)drv[0]).CheckName;
                if (title.Length > tlength)
                    tc.Text = title.Substring(0, tlength) + "...";
                else
                    tc.Text = title;
                tr.Cells.Add(tc);
                for (int i = 2; i <= dcount + 1; i++)
                {
                    tc = new TableCell();
                    tc.CssClass = "Td";
                    tc.BackColor = System.Drawing.Color.White;
                    tc.HorizontalAlign = HorizontalAlign.Right;
                    if (drv[i] != DBNull.Value)
                        temp = Check.GetCheck((int)drv[0]).TotalScore - (decimal)drv[i];
                    else
                        temp = Check.GetCheck((int)drv[0]).TotalScore;
                    total[i - 2] += temp;
                    tc.Text = "<a href=CheckView.aspx?CheckId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + temp.ToString("0") + "</a>";
                    tr.Cells.Add(tc);
                }
                tb.Rows.Add(tr);
                row++;
            }
        }
        return ros;
    }
    private int BindAwardTable(int mon, int ros, TableCell mTc)
    {
        if (Request["Award"] != null)
        {
            string Type = Request["T"];
            string departmentid = Request["DepartmentId"];
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("DECLARE @SQL nvarchar(max)");
            sql.AppendLine("SET @SQL = ''");
            sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '000' FROM Account_Departments WHERE Departmentid like '" + departmentid.Substring(0, 4) + Type + "%' order by departmentid ");
            sql.AppendLine("SET @SQL = 'SELECT a.AwardID, a.AwardName' + @SQL + ' FROM Awards a LEFT JOIN AwardItems b ON a.AwardID = b.AwardID WHERE YEAR(a.AwardDate)=" + Request.QueryString["Year"] + " AND MONTH(a.AwardDate)=" + mon + " and a.AwardID in (" + Request["Award"].Substring(0, Request["Award"].Length - 1) + ")  GROUP BY a.AwardID, a.AwardName, a.AwardDate ORDER BY a.AwardDate'");
            sql.AppendLine("EXEC(@SQL)");
            DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
            TableRow tr;
            TableCell tc;
            string title = "";
            foreach (DataRowView drv in dv)
            {
                tr = new TableRow();
                tr.CssClass = "grid_item";

                if (ros == 0)
                {
                    tr.Cells.Add(mTc);
                }
                ros++;

                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                title = row.ToString() + "、" + Award.GetAward((int)drv[0]).AwardName;
                if (title.Length > tlength)
                    tc.Text = title.Substring(0, tlength) + "...";
                else
                    tc.Text = title;
                tr.Cells.Add(tc);
                for (int i = 2; i <= dcount + 1; i++)
                {
                    tc = new TableCell();
                    tc.CssClass = "Td";
                    tc.BackColor = System.Drawing.Color.White;
                    tc.HorizontalAlign = HorizontalAlign.Right;
                    if (drv[i] != DBNull.Value)
                        temp = (decimal)drv[i];
                    else
                        temp = 0;
                    total[i - 2] += temp;
                    tc.Text = "<a href=AwardView.aspx?AwardId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + temp.ToString("0") + "</a>";
                    tr.Cells.Add(tc);
                }
                tb.Rows.Add(tr);
                row++;
            }
        }
        return ros;
    }
    private void BindTotalTable()
    {
        decimal Max = 0;
        TableRow tr = new TableRow();
        tr.CssClass = "grid_item";
        TableCell tc = new TableCell();
        TableCell tc1 = new TableCell();
        tc1.CssClass = "Td";
        tc1.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;合计";
        tc1.ColumnSpan = 2;
        tr.Cells.Add(tc1);
        for (int i = 0; i < dcount; i++)
        {
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = total[i].ToString("0");
            tr.Cells.Add(tc);
        }

        tb.Rows.AddAt(tb.Rows.Count, tr);

        tc1.RowSpan = 1;

        //Max = total.Max();
        //tr = new TableRow();
        //tr.ID = "44" + Guid.NewGuid();
        //tr.CssClass = "grid_item";
        //tc = new TableCell();
        //tc.CssClass = "Td";
        //tc.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;综合得分百分比";
        //tc.ColumnSpan = 2;
        //tr.Cells.Add(tc);
        //for (int i = 0; i < dcount; i++)
        //{
        //    tc = new TableCell();
        //    tc.CssClass = "Td";
        //    tc.ID = Guid.NewGuid() + i.ToString();
        //    tc.HorizontalAlign = HorizontalAlign.Right;
        //    tc.Text = total[i].ToString("0");
        //    tr.Cells.Add(tc);
        //}

        //tb.Rows.AddAt(tb.Rows.Count, tr);
    }
    private void BindChart()
    {
        string Type = Request["T"];
        string departmentid = Request["DepartmentId"];
        List<ResultInfo> results = new List<ResultInfo>();
        ResultInfo result = null;
        DataView dv = SplitPage.ExecuteSQL("SELECT DepartmentID, AliasName FROM Account_Departments WHERE Departmentid like '" + departmentid.Substring(0, 4) + Type + "%' order by departmentid ").Tables[0].DefaultView;
        for (int i = 0; i < dv.Count; i++)
        {
            result = new ResultInfo();
            result.DepartmentName = dv[i]["AliasName"].ToString();

            result.Score = total[i];

            results.Add(result);
        }
        Comparer c = new Comparer();
        results.Sort(c);
        UserChart.Titles["Title"].Text = Request.QueryString["Year"] + "年各单位综合得分统计表";
        UserChart.ChartAreas["Default"].AxisX.LabelsAutoFit = false;
        UserChart.Series["综合得分"].Points.DataBindXY(results, "DepartmentName", results, "Score");
    }
}